package com.hackintosh.chat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @description：
 * @author：twislyn
 * @date：Created in 2021/06/03
 * @modified By：
 * @version: 1.0
 */
public abstract class Customer extends JFrame implements ActionListener {

    protected String name;

    protected Medium medium;

    JTextField sendText;

    JTextArea receiveArea;

    public Customer(String name) {
        super(name);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMedium(Medium medium) {
        this.medium = medium;
    }

    protected void clientWindow(int x,int y){
        sendText = new JTextField(18);
        receiveArea = new JTextArea(10,18);
        receiveArea.setEditable(false);

        Container container = this.getContentPane();
        JPanel receivePanel = new JPanel();
        receivePanel.setBorder(BorderFactory.createTitledBorder("接受内容："));
        receivePanel.add(receiveArea);

        JScrollPane scrollPane = new JScrollPane(receivePanel);
        container.add(scrollPane,BorderLayout.NORTH);

        JPanel sendPanel = new JPanel();
        sendPanel.setBorder(BorderFactory.createTitledBorder("发送内容："));
        sendPanel.add(sendText);
        container.add(sendPanel,BorderLayout.SOUTH);

        sendText.addActionListener(this);
        this.setLocation(x,y);
        this.setSize(250,330);
        // 窗口大小不可调整
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String tempInfo = sendText.getText().trim();
        sendText.setText("");
        this.send(tempInfo);
    }

    public abstract void send(String sentence);

    public abstract void receive(String from, String sentence);
}
